package com.bite.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

@Slf4j
public class EncapsulatedRedisTemplate {

    private StringRedisTemplate redisTemplate;

    private static final String REDIS_SPLIT = ":";

    private static final String REDIS_DEFAULT_PREFIX = "default";

    public EncapsulatedRedisTemplate(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * 获取缓存
     * @param key
     * @return
     */
    public String get(String key) {
        try {
            return key == null ? null : redisTemplate.opsForValue().get(key);
        } catch (Exception e) {
            log.error("Redis GET ERROR，key: {}", key);
            return null;
        }
    }

    /**
     * 判断缓存是否存在
     * @param key
     * @return
     */
    public boolean exists(String key) {
        try {
            return key == null ? false : redisTemplate.hasKey(key);
        } catch (Exception e) {
            log.error("Redis EXISTS ERROR，key: {}", key);
            return false;
        }
    }

    /**
     * 设置缓存
     * @param key
     * @param value
     * @return
     */
    public boolean set(String key, String value) {
        try {
            if (key == null || value == null) {
                return false;
            }
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            log.error("Redis SET ERROR，key: {}，value: {}", key, value);
            return false;
        }
    }

    /**
     * 设置缓存，带过期时间，单位秒
     * @param key
     * @param value
     * @param expire
     * @return
     */
    public boolean set(String key, String value, long expire) {
        try {
            if (key == null || value == null || expire <= 0) {
                return false;
            }
            redisTemplate.opsForValue().set(key, value, expire);
            return true;
        } catch (Exception e) {
            log.error("Redis SET ERROR，key: {}，value: {}", key, value);
            return false;
        }
    }

    /**
     * 设置缓存，带过期时间，单位自定义
     * @param key
     * @param value
     * @param expire
     * @param timeUnit
     * @return
     */
    public boolean set(String key, String value, long expire, TimeUnit timeUnit) {
        try {
            if (key == null || value == null || expire <= 0) {
                return false;
            }
            redisTemplate.opsForValue().set(key, value, expire, timeUnit);
            return true;
        } catch (Exception e) {
            log.error("Redis SET ERROR，key: {}，value: {}", key, value);
            return false;
        }
    }

    public String buildKey(String prefix, String... args) {
        if (prefix == null) {
            prefix = REDIS_DEFAULT_PREFIX;
        }
        StringBuilder key = new StringBuilder();
        key.append(prefix);

        if (args != null) {
            for (String arg : args) {
                key.append(REDIS_SPLIT).append(arg);
            }
        }
        return key.toString();
    }

    //其他方法……
}
